{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn\n",
    "import torchaudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tf_gridnet import TF_GridNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = TF_GridNet(B=1,device=\"cpu\").to(\"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "checkpoint = torch.load(\"tf_gridnet.pth\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.load_state_dict(checkpoint[\"model_state_dict\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TF_GridNet(\n",
       "  (conv2d): Conv2d(2, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       "  (group_norm): GroupNorm(1, 24, eps=1e-05, affine=True)\n",
       "  (tf_grid_block): Sequential(\n",
       "    (0): IntraFrame(\n",
       "      (layerNorm): GroupNorm(1, 192, eps=1e-05, affine=True)\n",
       "      (bi_lstm): LSTM(192, 192, batch_first=True, bidirectional=True)\n",
       "      (deconv): ConvTranspose1d(384, 24, kernel_size=(8,), stride=(1,))\n",
       "    )\n",
       "    (1): InterFrame(\n",
       "      (layerNorm): GroupNorm(1, 192, eps=1e-05, affine=True)\n",
       "      (bi_lstm): LSTM(192, 192, batch_first=True, bidirectional=True)\n",
       "      (deconv): ConvTranspose1d(384, 24, kernel_size=(8,), stride=(1,))\n",
       "    )\n",
       "    (2): MultiHeadAttention(\n",
       "      (attentions): ModuleList(\n",
       "        (0): Attention(\n",
       "          (q_before): Sequential(\n",
       "            (0): Conv2d(24, 4, kernel_size=(1, 1), stride=(1, 1))\n",
       "            (1): PReLU(num_parameters=1)\n",
       "          )\n",
       "          (q_norm_cf): LayerNorm((4, 129), eps=1e-05, elementwise_affine=True)\n",
       "          (k_before): Sequential(\n",
       "            (0): Conv2d(24, 4, kernel_size=(1, 1), stride=(1, 1))\n",
       "            (1): PReLU(num_parameters=1)\n",
       "          )\n",
       "          (k_norm_cf): LayerNorm((4, 129), eps=1e-05, elementwise_affine=True)\n",
       "          (v_before): Sequential(\n",
       "            (0): Conv2d(24, 24, kernel_size=(1, 1), stride=(1, 1))\n",
       "            (1): PReLU(num_parameters=1)\n",
       "          )\n",
       "          (v_norm_cf): LayerNorm((24, 129), eps=1e-05, elementwise_affine=True)\n",
       "        )\n",
       "      )\n",
       "      (conv2d): Conv2d(24, 24, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (norm): PReLU(num_parameters=1)\n",
       "      (norm_cf): LayerNorm((24, 129), eps=1e-05, elementwise_affine=True)\n",
       "    )\n",
       "  )\n",
       "  (dconv): ConvTranspose2d(24, 4, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))\n",
       ")"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m.eval()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = torchaudio.load(\"mix_2.wav\")[0].unsqueeze(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 1, 44891])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with torch.no_grad():\n",
    "    y = m(x[:,:,:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 2, 44864])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "torchaudio.save(\"o1.wav\",y[0][0].unsqueeze(0), 8000)\n",
    "torchaudio.save(\"o2.wav\",y[0][1].unsqueeze(0), 8000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5,6,7\n"
     ]
    }
   ],
   "source": [
    "x = [5,6,7]\n",
    "\n",
    "y = \",\".join([str(i) for i in x])\n",
    "print(y)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
